package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

public class AppResult<T> {         //描述信息返回的结果数据

    @JsonInclude(JsonInclude.Include.ALWAYS)        //强制序列化,为null时也会被返回
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    //封装常用的方法
    //成功的

    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T>AppResult<T>success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);

    }

    public static <T>AppResult<T>success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),message,data);

    }

    // 失败的

    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(),message);

    }

    public static <T>AppResult<T> failed(T data){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.SUCCESS.getMessage(),data);

    }

    public static <T>AppResult<T>failed(ResultCode resultCode){
        return new AppResult<>(resultCode.getCode(),resultCode.getMessage());

    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    public AppResult(int code ,String message){
        this(code,message,null);
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
